#include<stdio.h>
#include<Matrix/LB_Matrix.h>
#include<Matrix/LB_MatrixOperator.h>
#include<Algorithm/subdivision_of_polygon.h>
#include <Matrix/LB_Sparse_Matrix.h>
#include<time.h>
#include<Math/polynomial.h>
#define Polynomial LB_Polynomial


// 0.7043 0.0 0.0 0.7682 0.0
// 0.0 0.9537 0.0 0.0 0.9924
// 0.0 -1.0721 0.1283 0.0 0.0
// -2.3 0.0 0.0 0.0 -0.372
// 0.0 0.0 0.0 0.9966 0.0

//以下是矩阵的逆

// 1.419849    0   0   0   -1.094450
// 9.134866    1.048548    0   2.797255    -7.041345
// 76.332734   8.761871    7.794232    23.374411   -58.838859
// 0   0   0   0   1.003412
// -8.778639   0   0   -2.688172   6.766758


//
// 0.0 0.0 0.0 0.6687 0.0 0.0
// 0.0 -7.041345 0.0 0.0 0.0 0.0
// 0.0 0.0 -2.3 0.0 -0.7043 0.0
// 0.0 0.9924 0.0 0.0 0.0 0.9157
// 0.0 0.0 0.0 0.0 0.5290 0.0
// 0.2611 0.0 0.1488 0.0 0.0 0.3476
// 
// 它的逆
// 
// 0   -0.204904   0.247781    -1.45385    0.329891    3.82995
// 0   -0.142018   0   0   0   0
// 0   0   -0.434783   0   -0.578861   0
// 1.495439    0   0   0   0   0
// 0   0   0   0   1.890359    0
// 0   0.153914    0   1.092061    0   0

// void test_sparse_matrix()
// {
//     LB_Sparse_Matrix* lsm=(LB_Sparse_Matrix*)malloc(sizeof(LB_Sparse_Matrix));
//     //lb_sparse_matrix_init(lsm);
//     lb_sparse_matrix_initn(lsm,5,5);

//     lb_sparse_matrix_insert(lsm, 0, 0, 0.7043);
//     lb_sparse_matrix_insert(lsm, 0, 3, 0.7682);
//     lb_sparse_matrix_insert(lsm, 1, 1, 0.9537);
//     lb_sparse_matrix_insert(lsm, 1, 4, 0.9924);
//     lb_sparse_matrix_insert(lsm, 2, 2, 0.1283);
//     lb_sparse_matrix_insert(lsm, 2, 1, -1.0721);
//     lb_sparse_matrix_insert(lsm, 3, 0, -2.3);
//     lb_sparse_matrix_insert(lsm, 3, 4, -0.372);
//     lb_sparse_matrix_insert(lsm, 4, 3, 0.9966);
//     // lb_sparse_matrix_insert(lsm,0,0,1);
//     // lb_sparse_matrix_insert(lsm,0,1,2);
//     LB_Sparse_Matrix*lsm1= lb_sparse_matrix_transepose(lsm);

//     LB_Sparse_Matrix*lsm2= lb_sparse_matrix_mult_sparse_matrix(lsm,lsm1);

    
//     lb_sparse_matrix_printself(lsm);
//     lb_sparse_matrix_printself(lsm1);
//     lb_sparse_matrix_printself(lsm2);

//     double * b=(double*)malloc(sizeof(double)*5);
//     memset(b,0,sizeof(double)*5);
//     b[0]=1.0;
//     double*x= compute_conjugate_gradient(lsm2,b);

//     double *y=lb_sparse_matrix_mult_array(lsm2,x);
//     for(int i=0;i<lsm2->rows;i++)
//     {
//         printf("%lf ",x[i]);
//     }

//     // x :5989.212608 678.395919 594.955062 1833.383735 -4616.609598 
// }

// void fillSparseMatrix(LB_Sparse_Matrix* matrix, int* rowPtr, int* colInd, double* values, int nnz) 
// {
//     for (int i = 0; i < nnz; i++) {
//         matrix[i].row = rowPtr[i];
//         matrix[i].col = colInd[i];
//         matrix[i].value = values[i];
//     }
// }
void test_sparse_matrix1()
{
   LB_Sparse_Matrix *lsm = (LB_Sparse_Matrix *)malloc(sizeof(LB_Sparse_Matrix));
    lb_sparse_matrix_initn(lsm, 5, 5);

    // 填充稀疏矩阵的数据
    lb_sparse_matrix_setrowsize(lsm, 0, 3);
    lsm->data[0][0].i = 0;
    lsm->data[0][0].v = 1.086170;
    lsm->data[0][1].i = 3;
    lsm->data[0][1].v = -1.619890;
    lsm->data[0][2].i = 4;
    lsm->data[0][2].v = 0.765588;

    lb_sparse_matrix_setrowsize(lsm, 1, 3);
    lsm->data[1][0].i = 1;
    lsm->data[1][0].v = 1.894401;
    lsm->data[1][1].i = 2;
    lsm->data[1][1].v = -1.022462;
    lsm->data[1][2].i = 3;
    lsm->data[1][2].v = -0.369173;

    lb_sparse_matrix_setrowsize(lsm, 2, 2);
    lsm->data[2][0].i = 1;
    lsm->data[2][0].v = -1.022462;
    lsm->data[2][1].i = 2;
    lsm->data[2][1].v = 1.165859;

    lb_sparse_matrix_setrowsize(lsm, 3, 3);
    lsm->data[3][0].i = 0;
    lsm->data[3][0].v = -1.619890;
    lsm->data[3][1].i = 1;
    lsm->data[3][1].v = -0.369173;
    lsm->data[3][2].i = 3;
    lsm->data[3][2].v = 5.428384;

    lb_sparse_matrix_setrowsize(lsm, 4, 2);
    lsm->data[4][0].i = 0;
    lsm->data[4][0].v = 0.765588;
    lsm->data[4][1].i = 4;
    lsm->data[4][1].v = 0.993212;

    // // 这里可以添加代码来测试或使用稀疏矩阵

    // // 释放内存
    printf("hereree\n");
    // SAFE_FREE(lsm);
    lb_sparse_matrix_free(lsm);
}


 

int main()
{
   test_sparse_matrix1();
   Polynomial* p=(Polynomial*)malloc(sizeof(Polynomial));
   lb_polynomial_initn(p,2);
   p->coefficients[0]=2.0; p->coefficients[1]=-2.0;   p->coefficients[2]=1.0;
   double root[4][2]={0};
   int rcount=getsolutions(p,0,root);
   for(int i=0;i<rcount;i++)
   {
      printf("%lf %lf\n",root[i][0],root[i][1]);
   }



    return 1;
}